package com.windy.dailynews.ui.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;

import com.windy.dailynews.R;
import com.windy.dailynews.model.News;
import com.windy.dailynews.model.Summary;
import com.windy.dailynews.presenter.HomeContract;
import com.windy.dailynews.presenter.impl.HomePresenter;
import com.windy.dailynews.ui.activity.NewsDetailActivity;
import com.windy.dailynews.ui.adapter.NewsListAdapter;
import com.windy.dailynews.ui.adapter.TopAdAdapter;
import com.windy.slideview.SlideViewPager;
import com.windy.xlistview.XListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by dingfeng on 2016/11/21.
 */
public class MainFragment extends Fragment implements HomeContract.View {
    private Context mContext;
    private HomeContract.Presenter mPresenter;
    private TopAdAdapter mTopAdAdapter;
    private NewsListAdapter mListAdapter;
    private SlideViewPager mSlideViewPager;
    private List<Summary> mSummaryList;

    @Bind(R.id.listView)
    XListView mListView;
    @Bind(R.id.loading)
    View loading;
    @Bind(R.id.load_failed)
    View load_failed;

    public static MainFragment newInstance() {
        return new MainFragment();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main, container, false);
        ButterKnife.bind(this, view);
        mContext = getActivity();
        init();
        return view;
    }

    private void init() {
        load_failed.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.loadData();
            }
        });
        View header = LayoutInflater.from(mContext).inflate(R.layout.include_slide_view, null);
        mSlideViewPager = (SlideViewPager) header.findViewById(R.id.slideViewPager);
        mListView.addHeaderView(header);
        initListView();
        mSummaryList = new ArrayList<>();
        new HomePresenter(this);
        mPresenter.start();
    }

    private int lastVisibleItem;
    private boolean IsLoadingMore = false;

    private void initListView() {
        mListView.setOnItemClickListener(onItemClickListener);
        mListView.setPullLoadEnable(true);
        mListView.setPullRefreshEnable(false);
        mListView.setXListViewListener(new XListView.IXListViewListener() {

            @Override
            public void onRefresh() {

            }

            @Override
            public void onLoadMore() {
                mPresenter.loadMore();
            }
        });
        mListView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if (lastVisibleItem >= mListAdapter.getCount() && scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE && !IsLoadingMore) {
                    mPresenter.loadMore();
                    IsLoadingMore = true;
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                lastVisibleItem = firstVisibleItem + visibleItemCount - 1 ;
            }
        });

    }

    private AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent intent = new Intent(mContext, NewsDetailActivity.class);
            int newsId = mSummaryList.get(position - 2).getId();
            intent.putExtra("newsId", newsId);
            startActivity(intent);
        }
    };

    @Override
    public void showTopAd(List<News.Story> list) {
        mTopAdAdapter = new TopAdAdapter(mContext, list);
        mSlideViewPager.setIndicator(list.size());
        mTopAdAdapter.setInfiniteLoop(true);
        mSlideViewPager.setAdapter(mTopAdAdapter);
    }

    @Override
    public void showNewsList(final List<Summary> list) {
        mSummaryList.addAll(list);
        mListAdapter = new NewsListAdapter(mContext, mSummaryList);
        mListView.setAdapter(mListAdapter);
    }

    @Override
    public void addBeforeList(List<Summary> list) {
        mSummaryList.addAll(list);
        mListAdapter.notifyDataSetChanged();
        mListView.stopLoadMore();
        IsLoadingMore = false;
    }

    @Override
    public void setLoading() {
        loading.setVisibility(View.VISIBLE);
        load_failed.setVisibility(View.GONE);
    }

    @Override
    public void hideLoading() {
        loading.setVisibility(View.GONE);
        load_failed.setVisibility(View.GONE);
    }

    @Override
    public void setError() {
        loading.setVisibility(View.GONE);
        load_failed.setVisibility(View.VISIBLE);
        IsLoadingMore = false;
    }

    @Override
    public void setPresenter(HomeContract.Presenter presenter) {
        mPresenter = presenter;
    }

}
